package enumandlambda;

import java.time.LocalDateTime;

/**
 * @author shkstart
 * 内存可见性问题
 * @create 2022-12-03-16:47
 */
public class ThreadDemo17 {
    //全局变量（类级别）
    private static volatile boolean flag = true;

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            System.out.println("线程1：开始执行！"+ LocalDateTime.now());
            while(flag){

            }
            System.out.println("线程1：执行结束"+LocalDateTime.now());
        });
        t1.start();

        Thread t2 = new Thread(()->{
            //休眠1S
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("相乘2：修改flag = false"+LocalDateTime.now());
            flag = false;
        });
        t2.start();


    }
}
